import { CommandId } from '~types/global-state'
import { WindowCommand } from '#/shortcuts/index'

export const windowCommand = new Map<CommandId, WindowCommand>()

windowCommand.set('cmd:tab:new', ({ tabManager }) => {
  tabManager.createTabs('top')
})

windowCommand.set('cmd:tab:close', ({ layoutTab, sourceTab }) => {
  console.log('[shortcuts] tab:close')
  if (layoutTab === sourceTab) return
  sourceTab.close()
})

windowCommand.set('cmd:tab:reload', ({ tabManager, sourceTab }) => {
  if (sourceTab) sourceTab.reload()
  else tabManager.activeTab?.tab.reload()
})

windowCommand.set('cmd:devtools:toggle', ({ tabManager, sourceTab }) => {
  if (sourceTab) sourceTab.view.webContents.toggleDevTools()
  else tabManager.activeTab?.tab.view.webContents.toggleDevTools()
})

windowCommand.set('cmd:settings:open', ({ layoutTab }) => {
  console.log('[shortcuts] settings:open')
  layoutTab.view.webContents.send('cmd:settings:open')
})

windowCommand.set('cmd:find:toggle', ({ layoutTab, sourceTab }) => {
  if (sourceTab === layoutTab) return
  layoutTab.view.webContents.send('cmd:find:toggle', sourceTab.view.webContents.id)
})
